<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="照片" prop="image">
        <!-- TODO 文件上传 -->
        <el-upload
          class="avatar-uploader"
          :action="this.$baseUrl+'/easuser/file/upload'"
          :headers="headers"
          :show-file-list="false"
          :on-success="uploadcallback"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        
        <!-- <el-input v-model="dataForm.image" placeholder="照片">
        
        </el-input> -->
      </el-form-item>
    <el-form-item label="教师" prop="teacherId">
      
      <el-select class="select" v-model="dataForm.teacherId" filterable placeholder="请选择">
        <el-option
          v-for="item in teacherList"
          :key="item.tid"
          :label="item.name +' 工号: ' + item.tid"
          v-html="item.name +'&nbsp;&nbsp;&nbsp;&nbsp; 工号: ' + item.tid"
          :value="item.tid">
        </el-option>
      </el-select>
     
    </el-form-item>
    
    <el-form-item label="介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" type="textarea" autosize placeholder="介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { teacherApi, teacherstyleApi } from '@/api/teacher'
import { getToken } from '@/utils/auth'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          teacherId: '',
          image: '',
          introduce: ''
        },
        headers:{
          "token":getToken()
        },
        imageUrl: '',
        teacherList:[],
        dataRule: {
          teacherId: [
            { required: true, message: '教师id不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            teacherstyleApi.Info(id)
            .then(({teacherStyle}) => {
                this.dataForm = teacherStyle
                this.imageUrl = teacherStyle.image
            })
          }
        })
        teacherApi.searchTeacher()
        .then(({data})=>{
          this.teacherList = data
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            teacherstyleApi.saveOrUpdate(this.dataForm,!this.dataForm.id ? 'save' : 'update')
            .then((data) => {
              
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })  
            })
          }
        })
      },
      uploadcallback(response, file, fileList){
        if(response.code===20000){
          this.imageUrl=response.data
          this.dataForm.image = response.data
        }else{
          this.$message.error('上传出错，请联系管理员');
        }
       
        console.log(response);

      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 4;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>
<style lang="scss">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  </style>
